# captcha.py
from flask import Blueprint, jsonify
from flask_mail import Message, Mail
from exts import mail  # 从 config.py 导入 mail 配置
from random import randint

# 创建蓝图
captcha_bp = Blueprint('captcha', __name__)


# 邮件发送视图
@captcha_bp.route('/email')
def email():
    verify = str(randint(0, 9999)).zfill(4)  # 生成随机验证码
    # 创建邮件对象
    message = Message(
        subject='测试邮件',
        recipients=['courage.eroica@gmail.com'],
        html=f"""
        <!DOCTYPE html>
        <html>
        <head>
            <style>
                .container {{
                    font-family: Arial, sans-serif;
                    text-align: center;
                    padding: 20px;
                }}
                .code {{
                    font-size: 24px;
                    font-weight: bold;
                    color: #ff5733;
                }}
            </style>
        </head>
        <body>
            <div class="container">
                <h3>您的验证码为：</h3>
                <p class="code">{verify}</p>
                <p>请妥善保管！</p>
            </div>
        </body>
        </html>
        """
    )

    # 发送邮件
    mail.send(message)
    return jsonify({'message': '邮件发送成功', 'verify': verify})
